﻿using System.Collections.Generic;
using BruteForce.Animation;
using Bruteforce.Animation;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BruteForce.NewManagers
{
    public class TransitionManager : Manager
    {
        public static TransitionManager Instance = new TransitionManager();

        private readonly HashSet<Transition> transitions = new HashSet<Transition>();
        private List<Transition> _toRemove = new List<Transition>();

        public override void Draw(GameTime gameTime, BFGame game)
        {
        }

        protected override void _Update(GameTime gameTime, BFGame game)
        {
            _toRemove = new List<Transition>();
            foreach (Transition t in transitions)
            {
                if (1.0 - t.Value < 0.00000001)
                {
                    _toRemove.Add(t);
                }
                else t.Interpolate(gameTime);
            }

            while (_toRemove.Count > 0)
            {
                Transition v = _toRemove[0];
                _toRemove.RemoveAt(0);
                transitions.Remove(v);
                v.ReportFinish();
            }
        }

        public override void LoadContent(ContentManager content)
        {
            ArrowTransition.ArrowTexture = content.Load<Texture2D>(@"Textures/TileSets/arrow");
            ExplosionTransition.Textures = new List<Texture2D>();
            for (int i=1; i<49; i++)
            {
                var temp = "Textures/Animation/Explosion/explode_" + i;
                ExplosionTransition.Textures.Add(content.Load<Texture2D>(temp));
            }
        }

        public override void Initialize()
        {
        }

        public void AddTransition(Transition t)
        {
            transitions.Add(t);
            _toRemove.Remove(t);
        }
    }
}